﻿using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using POS.BusinessEntities;
using POS.DataObjects;

namespace POS.DataObjectsTest
{
    [TestClass]
    public class DOProveedorTest
    {
        #region Declarations

        private static Database db;
        private const string TestUser = "testUser";
        private const int EmpresaId = 1;
        private const int TipoProveedorId = 1;
        
        #endregion

        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
            CleanupTestData();
            SetupTestData();
        }

        [ClassCleanup]
        public static void MyClassCleanup()
        {
            CleanupTestData();
        }

        #region Properties: Non-Public

        private static Database Db
        {
            get
            {
                if (db == null)
                    db = DatabaseFactory.CreateDatabase();
                return db;
            }
        }

        #endregion

        #region Test Methods

        [TestMethod]
        public void GetListProveedorSinFiltro()
        {
            CreateProveedor("TestName");
            var ds = Db.ExecuteDataSet(CommandType.Text, "select proveedorId id, nombre, cedula, diasCredito, direccion, email, limiteCredito, notas, paisId, provinciaId, telefono, tipoId, activo, modificadoPor from proveedor where empresaId = " + EmpresaId + " and activo=1");
            var filtro = string.Empty;

            var results = new DOProveedor().GetList(EmpresaId, filtro);
            Helper.AssertGetListResult(ds, results);
        }

        [TestMethod]
        public void SaveProveedorNew()
        {
            const string name = "TestName";
            const string cedula = "ced";
            const int diasCredito = 4;
            const string direccion = "dir";
            const string email = "email";
            const decimal limiteCredito = (decimal)500.6;
            const string notas = "notas";
            const int paisId = 1;
            const int provinciaId = 2;
            const string telefono = "telef";
            const int tipoId = 1;
            
            var entity = new BEProveedor
            {
                Nombre = name,
                Cedula = cedula,
                DiasCredito = diasCredito,
                Direccion = direccion,
                Email = email,
                LimiteCredito = limiteCredito,
                Notas = notas,
                PaisId = paisId,
                ProvinciaId = provinciaId,
                Telefono = telefono,
                TipoId = tipoId
            };
            Db.ExecuteNonQuery(CommandType.Text, "delete proveedor where empresaId = " + EmpresaId);
            new DOProveedor().Save(entity, EmpresaId, TestUser);

            var ds = Db.ExecuteDataSet(CommandType.Text, "select * from proveedor where empresaId=" + EmpresaId);
            Assert.IsNotNull(ds);
            Assert.AreEqual(1, ds.Tables.Count);
            Assert.AreEqual(1, ds.Tables[0].Rows.Count);
            var row = ds.Tables[0].Rows[0];
            Assert.IsTrue((int)row["ProveedorId"] > 0);
            Assert.AreEqual(name, row["Nombre"]);
            Assert.AreEqual(cedula, row["cedula"]);
            Assert.AreEqual(diasCredito, row["diasCredito"]);
            Assert.AreEqual(direccion, row["direccion"]);
            Assert.AreEqual(email, row["email"]);
            Assert.AreEqual(limiteCredito, row["limiteCredito"]);
            Assert.AreEqual(notas, row["notas"]);
            Assert.AreEqual(paisId, row["paisId"]);
            Assert.AreEqual(provinciaId, row["provinciaId"]);
            Assert.AreEqual(telefono, row["telefono"]);
            Assert.AreEqual(tipoId, row["tipoId"]);
            Assert.AreEqual(true, row["Activo"]);
            Assert.AreEqual(TestUser, row["ModificadoPor"]);
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaModificacion"].ToString()).ToShortDateString());
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaCreacion"].ToString()).ToShortDateString());
        }

        [TestMethod]
        public void SaveProveedorExisting()
        {
            const string name = "TestName";
            const string cedula = "ced";
            const int diasCredito = 4;
            const string direccion = "dir";
            const string email = "email";
            const decimal limiteCredito = (decimal)500.6;
            const string notas = "notas";
            const int paisId = 1;
            const int provinciaId = 2;
            const string telefono = "telef";
            const int tipoId = 1;

            DeleteProveedor(name);
            var id = CreateProveedor(name);
            var expectedName = name + DateTime.Now.Ticks;
            var expectedUser = TestUser + DateTime.Now.Ticks;
            var entity = new BEProveedor
            {
                Id = id,
                Nombre = expectedName,
                Cedula = cedula,
                DiasCredito = diasCredito,
                Direccion = direccion,
                Email = email,
                LimiteCredito = limiteCredito,
                Notas = notas,
                PaisId = paisId,
                ProvinciaId = provinciaId,
                Telefono = telefono,
                TipoId = tipoId
            };
            new DOProveedor().Save(entity, EmpresaId, expectedUser);

            var ds = Db.ExecuteDataSet(CommandType.Text, "select * from proveedor where empresaId=" + EmpresaId);
            Assert.IsNotNull(ds);
            Assert.AreEqual(1, ds.Tables.Count);
            Assert.AreEqual(1, ds.Tables[0].Rows.Count);
            var row = ds.Tables[0].Rows[0];
            Assert.IsTrue((int)row["ProveedorId"] > 0);
            Assert.AreEqual(expectedName, row["Nombre"]);
            Assert.AreEqual(cedula, row["cedula"]);
            Assert.AreEqual(diasCredito, row["diasCredito"]);
            Assert.AreEqual(direccion, row["direccion"]);
            Assert.AreEqual(email, row["email"]);
            Assert.AreEqual(limiteCredito, row["limiteCredito"]);
            Assert.AreEqual(notas, row["notas"]);
            Assert.AreEqual(paisId, row["paisId"]);
            Assert.AreEqual(provinciaId, row["provinciaId"]);
            Assert.AreEqual(telefono, row["telefono"]);
            Assert.AreEqual(tipoId, row["tipoId"]);
            Assert.AreEqual(true, row["Activo"]);
            Assert.AreEqual(expectedUser, row["ModificadoPor"]);
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaModificacion"].ToString()).ToShortDateString());
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaCreacion"].ToString()).ToShortDateString());
        }


        #endregion

        #region Test Utilities
        
        private static void DeleteProveedor(string name)
        {
            Db.ExecuteNonQuery(CommandType.Text, "delete proveedor where empresaId = " + EmpresaId + " and nombre='" + name + "'");
        }

        private static int CreateProveedor(string name)
        {
            const int id = 999;
            Db.ExecuteNonQuery(CommandType.Text, "delete Proveedor where ProveedorId = " + id);
            var sql = "Insert into Proveedor " +
                      " (ProveedorId, EmpresaId, Activo, Nombre, Cedula, Telefono, Email, PaisId, ProvinciaId, TipoId," +
                      "  Direccion, Notas, DiasCredito, LimiteCredito, FechaCreacion, FechaModificacion, ModificadoPor) " +
                      "Values (" + id + ", " + EmpresaId + ", 1,'" + name + "', 'ced', 'telef', 'email', 1, 2, " + TipoProveedorId + "," +
                      "        'Dir', 'notas', 5, 100.5, getdate(), getdate(), 'TestUser')";
            Db.ExecuteNonQuery(CommandType.Text, sql);
            return id;
        }

        private static void SetupTestData()
        {
            var sql = "Insert into TipoProveedor (TipoProveedorId, EmpresaId, Activo, Nombre, FechaCreacion, FechaModificacion, ModificadoPor) " +
                      "Values (" + TipoProveedorId + ", " + EmpresaId + ", 1, 'TestTipoProveedor', getdate(), getdate(), 'TestUser')";
            Db.ExecuteNonQuery(CommandType.Text, sql);
        }

        private static void CleanupTestData()
        {
           
        }

        #endregion
    }
}
